/*
 * Copyright 2012 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package wbbs.web;

import cn.webwheel.Action;
import cn.webwheel.results.TemplateResult;
import com.google.inject.Inject;
import wbbs.domain.User;
import wbbs.service.LoginService;
import wbbs.service.UserService;
import wbbs.service.Utils;

import java.sql.SQLException;
import java.util.Arrays;

public class login extends BaseAction {

    public String url = "/";

    @Inject
    UserService userService;

    @Inject
    LoginService loginService;

    @Action
    public Object html() {
        return new TemplateResult(this);
    }

    @Action(".do")
    public Object execute(String id, String pwd) throws SQLException {
        User user = userService.findUser(id);
        notNull(user, "用户名错误");
        byte[] md5 = Utils.md5(pwd);
        affirm(Arrays.equals(md5, user.pwd), "密码错误");
        loginService.setLoginUserId(id);
        return ok();
    }
}
